package cn.hcbycloud.auth.service;

import cn.hcbycloud.auth.bo.PermissionBO;
import cn.hcbycloud.auth.bo.PermissionContainResourceBO;
import cn.hcbycloud.auth.bo.ResourceActionBO;
import cn.hcbycloud.auth.bo.RoleBO;
import cn.hcbycloud.auth.mapper.PermissionMapper;
import cn.hcbycloud.auth.model.Permission;
import cn.hcbycloud.common.core.exception.CustomizeException;
import cn.hcbycloud.common.core.utils.ObjectUtils;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * 权限表
 *
 * @author gaoheng
 * @date 2022-01-21 23:44:11
 */
@Service
@AllArgsConstructor
public class PermissionService extends ServiceImpl<PermissionMapper, Permission> {
    private PermissionMapper permissionMapper;
    /**
     * 资源对象服务
     */
    private ResourceService resourceService;

    /**
     * 添加权限信息
     * @param permissionBo
     * @param tenantId
     * @return
     */
    public String addPermissionInfo(PermissionBO permissionBo, String tenantId) {
        Permission permission = null;
        try {
            permission = ObjectUtils.cloneObject(permissionBo, Permission.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        permission.preInsert();
        permission.setTenantId(tenantId);
        permissionMapper.insert(permission);
        return permission.getId();
    }

    /**
     * 根据角色ID列表获取权限列表
     *
     * @param roleIds
     * @param tenantId
     * @return
     */
    public List<PermissionBO> getPermissionInfoByRoleIds(List<String> roleIds, String tenantId) {
        List<Permission> permissionList = permissionMapper.selectPermissionInfoByRoleIds(roleIds, tenantId);
        return convertPermissionList(permissionList);
    }

    /**
     * @param roleCodes
     * @param tenantId
     * @return
     */
    public List<PermissionBO> getPermissionInfoByRoleCodes(List<String> roleCodes, String tenantId) {
        List<Permission> permissionList = permissionMapper.selectPermissionInfoByRoleCodes(roleCodes, tenantId);
        return convertPermissionList(permissionList);
    }

    /**
     * 根据角色对象列表获取权限及资源
     *
     * @param roleBoList
     * @param tenantId
     * @return
     */
    public PermissionContainResourceBO getPermissionContainResourceByRoles(List<RoleBO> roleBoList, String tenantId) {
        // 返回角色对应的权限属性列表
        List<PermissionBO> permissionBoList = null;
        if (CollUtil.isNotEmpty(roleBoList)) {
            List<String> roleCodes = CollUtil.newArrayList();
            roleBoList.forEach(item -> roleCodes.add(item.getRoleCode()));
            permissionBoList = this.getPermissionInfoByRoleCodes(roleCodes, tenantId);
        }
        // 返回权限对应的资源行为对象列表
        List<ResourceActionBO> resourceActionBoList = null;
        if (CollUtil.isNotEmpty(permissionBoList)) {
            List<String> permissionCodes = CollUtil.newArrayList();
            permissionBoList.forEach(item -> permissionCodes.add(item.getPermissionCode()));
            resourceActionBoList = resourceService.getResourceActionByPermissionCodes(permissionCodes, tenantId);
        }
        return new PermissionContainResourceBO(permissionBoList, resourceActionBoList);
    }

    /**
     * 根据用户ID获取权限编码集合
     * @param userId
     * @return
     */
    public Set<String> getPermissionsByUserId(String userId,String tenantId) {
        return permissionMapper.selectPermissionsByUserId(userId,tenantId);
    }

    /**
     * BO对象转换
     *
     * @param permissionList
     * @return
     */
    private List<PermissionBO> convertPermissionList(List<Permission> permissionList) {
        List<PermissionBO> permissionBoList = null;
        try {
            permissionBoList = ObjectUtils.cloneObjList(permissionList, PermissionBO.class);
        } catch (Exception e) {
            throw new CustomizeException("获取权限信息错误");
        }
        return permissionBoList;
    }


}
